<?php
/**
 * @Author: hpp
 * @Date:   2018-12-04 17:57:46
 * @Last Modified by:   hpp
 * @Last Modified time: 2018-12-05 18:07:03
 */
namespace app\admin\controller;

use think\Request;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Role;
use app\admin\model\AdminRole;

class Admin extends BaseController
{
    /**
     * 管理员列表
     */
    public function index()
    {
        $list = AdminModel::order('id','asc')->paginate(AdminModel::PAGESIZE);
        $roleName = Role::column('name','id');
        $adminRoleId = AdminRole::column('role_id','admin_id');
        $role = Role::field('id,name')->select();
        return $this->fetch('admin/index',compact('list','role','roleName','adminRoleId'));
    }

    /**
     * 保存
     */
    public function save(Request $request)
    {
        $data = $request->param();
//        parse_str($str,$data);
        $res = $this->validate($data,'app\admin\validate\Admin.add');
        if($res !== true){
            return $this->error($res);
        }
        unset($data['password_confirm']);
        $res = $res and $insertRes = AdminModel::create($data);
        $adminRole['admin_id'] = $insertRes->id;
        $adminRole['role_id'] = $data['role_id'];
        $res = $res and AdminRole::create($adminRole);
        if(!$res){
            return $this->error('添加失败');
        }
        return $this->success('添加成功');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $data = AdminModel::field('id,username,email')->find(intval($id));
        return json(['code' =>1, 'data' => $data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();
        unset($data['role_id']);
        $res = $this->validate($data,'app\admin\validate\Admin.edit');
        if($res !== true) return $this->error($res);
        if(!empty($data['password'])){
            if($data['password'] != $data['password_confirm']){
                return $this->error('两次密码不一致');
            }
            if((mb_strlen($data['password']) > 12 || mb_strlen($data['password'])<6) || (mb_strlen($data['password_confirm']) > 12 || mb_strlen($data['password_confirm'])<6)){
                return $this->error('密码长度要求在6-12位之间');
            }
            $data['password'] = md5_password($data['password']);
            unset($data['password_confirm']);
        }else{
            unset($data['password'],$data['password_confirm']);
        }
        unset($data['id']);
        $updateRes = AdminModel::where('id',$id)->update($data);
        if($updateRes){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }

    public function updateStatus($id,$status)
    {
        if (!in_array($status, [0,1])) {
            return $this->error('状态错误');
        }
        $status = $status == 0 ? 1 : 0;
        $res = AdminModel::where('id',$id)->update(['status' => $status]);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int
     */
    public function delete($id)
    {
        $res = AdminModel::destroy($id);
        $res = $res and AdminRole::where('admin_id',$id)->delete();
        if($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    public function deletes(){
        $ids = input('get.ids');
        if(empty($ids) || !is_array($ids)){
            return $this->error('请选择要删除的数据');
        }
        $res = AdminModel::destroy($ids);
        $res = $res and AdminRole::where('admin_id','in',$ids)->delete();
        if($res){
            return json(['code' => 1,'msg' => '删除成功','data' => $ids]);
        }else{
            return $this->error('删除失败');
        }
    }

    public function modifyRole(Request $request){
        $id = $request->id;
        $role_id = $request->role_id;
        $res = AdminRole::where('admin_id',$id)->update(['role_id' => $role_id]);
        if($res){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
}
